package com.yunfeng.supercall.activity;

import java.util.Collections;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.ly.quickdev.library.utils.JsonUtils;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.bean.ContactsBaseBean;
import com.yunfeng.supercall.bean.SuperFriendsBean;
import com.yunfeng.supercall.httputils.MyAjaxCallBack;

/**
 * 附近的超友
 * 
 * @author 洋
 * 
 */
public class NearBySuperFriendsListActivity extends
		BaseStickyHeaderListActivity<SuperFriendsBean> {

	private int mType;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		startActivity(new Intent(this, FriendsDetailActivity.class).putExtra(
				KEY_DATA, mList.get(position)).putExtra(KEY_DONT_SHOW_NUMBER,
				true));
	}

	@Override
	public void loadData() {
		// TODO Auto-generated method stub
		mType = getIntent().getIntExtra(KEY_DATA, 0);
		String url = null;
		if (mType == 0) {
			url = FUNCTION_GET_NEAR_USER_LIST;
			setTitleBar(R.string.super_number_nearby);
		} else {
			url = FUNCTION_GET_NEAR_BUSSINESS;
			setTitleBar(R.string.enter_prise_nearby);
		}
		JSONObject json = getBaseParams(getActivity());
		try {
			json.put(KEY_PAGE_NUM, 0);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mHttpClient.post(url, json, new MyAjaxCallBack() {
			@Override
			public void onReceiveError(String msg, String errorCode) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onReceiveData(String data, String msg) {
				// TODO Auto-generated method stub
				mList = JsonUtils.parseList(data, SuperFriendsBean.class);
				new Thread() {
					public void run() {
						Collections.sort(mList);
						mHandler.sendEmptyMessage(0);
					};
				}.start();

			}
		});
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mHandler.removeMessages(0);
	}

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			setListAdapter();
		};
	};

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (convertView == null) {
			convertView = getLayoutInflater().inflate(
					R.layout.list_item_system_friends, null);
		}
		ContactsBaseBean.setupSingleLineContactsView(convertView,
				mList.get(position));
		return convertView;
	}

}
